package com.canbot.u05.update;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Handler;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 解压安装任务类
 *
 * @author lucky
 */
public class UnzipInstallTask extends AsyncTask<String, Integer, Boolean> {

        private boolean rsboolean = false;

        private String zipPath = null;

        private File[] arrStrPath;

        private Handler mHandler;

        private String tag;

        private Context context;

        private int m = 0;

        private boolean mRunning;

        private List<ApkFileInfo> apkFileInfos = new ArrayList<ApkFileInfo>();

        public UnzipInstallTask(Context context, String tag, Handler mHandler,
                                Boolean mRunning) {
                this.context = context;
                this.tag = tag;
                this.mRunning = mRunning;
                this.mHandler = mHandler;
        }

        @Override
        protected Boolean doInBackground(String... params) {

                if (isCancelled()) {
                        return (null); // don't forget to terminate this method
                }
                zipPath = params[0];
                try {
                        if (ZipUtils.unZipFiles(ConstUtils.ZIP_APP_PATH + params[0],
                                ConstUtils.Path_SD + "/")) {
//                Message msg = mHandler.obtainMessage(6, "解压完毕");
//                mHandler.sendMessage(msg);
                                // Toast.makeText(MainActivity.this, "解压完成",
                                // Toast.LENGTH_SHORT).show();
                                File fileapk = new File(ConstUtils.PATH_UPDATE_APK);
                                if (!fileapk.exists()) {
//                    Log.e(tag, "没有apk需要更新");
                                        mHandler.sendEmptyMessage(1);
                                        rsboolean = true;
                                        return rsboolean;
                                }

                                arrStrPath = fileapk.listFiles();
                                if (arrStrPath.length <= 0) {
//                    Log.e(tag, "没有apk需要更新");
                                        mHandler.sendEmptyMessage(1);
//                    Log.e(tag, "rsboolean<=0");
                                        rsboolean = true;
                                        return rsboolean;
                                }
                                PackageManager pm = context.getPackageManager();
                                for (int i = 0; i < arrStrPath.length; i++) {
                                        PackageInfo info = pm.getPackageArchiveInfo(
                                                arrStrPath[i].getAbsolutePath(),
                                                PackageManager.GET_ACTIVITIES);
                                        String pkname = null;
                                        if (info != null) {
                                                pkname = info.packageName;
//                        Log.e(tag, "packageName=" + pkname);
                                        }
                                        ApkFileInfo apkFileInfo = new ApkFileInfo();
                                        apkFileInfo.setApkFile(arrStrPath[i]);
                                        apkFileInfo.setPackagename(pkname);
                                        apkFileInfos.add(apkFileInfo);
                                }

                                for (int i = m; i < apkFileInfos.size() && mRunning; i++) {
//                    Log.e(tag, "开始安装apk"
//                            );
//                    Log.e(tag, "apkname="
//                            + apkFileInfos.get(i).getApkFile().getName());
                                        if (apkFileInfos.get(i).getPackagename() != null) {
                                                // myrobot = apkFileInfos.get(i).getApkFile();
//                        RobotUtil.slientInstall(
//                                apkFileInfos.get(i).getApkFile(), context);
                                                RobotUtil.install(apkFileInfos.get(i).getApkFile(), context);
//                        mHandler.sendEmptyMessage(5);
                                                rsboolean = false;
                                                return rsboolean;
                                        }

                                        // RobotUtil.slientInstall(apkFileInfos.get(i).getApkFile(),context);

                                        // if (RobotUtil0
                                        // .slientInstall(apkFileInfos.get(i).getApkFile(),context))
                                        // {
                                        // Log.e(tag, apkFileInfos.get(i).getApkFile().getName()
                                        // + "安装成功");
                                        // Message msg = mHandler.obtainMessage(6,
                                        // apkFileInfos.get(i).getApkFile().getName()
                                        // + "安装成功");
                                        // mHandler.sendMessage(msg);
                                        // } else {
                                        // Log.e(tag, apkFileInfos.get(i).getApkFile().getName()
                                        // + "安装失败");
                                        // Message msg = mHandler.obtainMessage(6,
                                        // apkFileInfos.get(i).getApkFile().getName()
                                        // + "安装失败");
                                        // mHandler.sendMessage(msg);
                                        // }
                                }
                                rsboolean = true;
                                return rsboolean;
                        }
                }
                catch (IOException e) {
                        e.printStackTrace();
                }

                return rsboolean;
        }

        @Override
        protected void onPostExecute(Boolean result) {
                if (isCancelled()) {
                        return; // don't forget to terminate this method
                }
                mRunning = false;
                if (zipPath != null) {
                        File dltfile = new File(ConstUtils.ZIP_APP_PATH + zipPath);
                        if (dltfile.exists()) {
                                dltfile.delete();
                        }
                }
                for (int i = m; i < apkFileInfos.size(); i++) {
                        if (apkFileInfos.get(i).getPackagename() != null) {
                                continue;
                        }

                        if (apkFileInfos.get(i).getApkFile().exists()) {
                                apkFileInfos.get(i).getApkFile().delete();
                        }
                }

        }

}